home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_11_06 / 1106090a < prev    next >
Text File  |  1993-04-06  |  5KB  |  161 lines

  1.      /*******************************************
  2.      *
  3.      *   edge_region(..
  4.      *
  5.      *   This function segments an image by
  6.      *   growing regions inside of edges.
  7.      *   The steps are:
  8.      *      . detect edges
  9.      *      . threshold edge output to a
  10.      *        percent value
  11.      *      . remove edges from consideration
  12.      *      . grow regions
  13.      *
  14.      *******************************************/
  15.  
  16. edge_region(in_name, out_name, the_image, out_image,
  17.             il, ie, ll, le, edge_type, min_area,
  18.             max_area, diff, percent, set_value,
  19.             erode)
  20.    char     in_name[], out_name[];
  21.    float    percent;
  22.    int      edge_type, il, ie, ll, le;
  23.    short    diff, erode, 
  24.             max_area, min_area, 
  25.             set_value,
  26.             the_image[ROWS][COLS],
  27.             out_image[ROWS][COLS];
  28. {
  29.  
  30.    int    a, b, count, i, j, k,
  31.           length, width;
  32.    short  cutoff;
  33.    struct tiff_header_struct image_header;
  34.    unsigned long histogram[GRAY_LEVELS+1];
  35.  
  36.    if(does_not_exist(out_name)){
  37.       printf("\n\n output file does not exist %s",
  38.              out_name);
  39.       read_tiff_header(in_name, &image_header);
  40.       round_off_image_size(&image_header,
  41.                            &length, &width);
  42.       image_header.image_length = length*ROWS;
  43.       image_header.image_width  = width*COLS;
  44.       create_allocate_tiff_file(out_name, &image_header,
  45.                                 out_image);
  46.    }  /* ends if does_not_exist */
  47.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  48.  
  49.       /***************************
  50.       *
  51.       *   Detect the edges.  Do
  52.       *   not threshold.
  53.       *
  54.       ****************************/
  55.  
  56.    if(edge_type == 1  ||
  57.       edge_type == 2  ||
  58.       edge_type == 3)
  59.       detect_edges(in_name, out_name, the_image,
  60.                    out_image, il, ie, ll, le,
  61.                    edge_type, 0, 0);
  62.  
  63.    if(edge_type == 4){
  64.       quick_edge(in_name, out_name, the_image,
  65.                  out_image, il, ie, ll, le,
  66.                  0, 0);
  67.    }  /* ends if 4 */
  68.  
  69.    if(edge_type == 5){
  70.       homogeneity(in_name, out_name, the_image,
  71.                  out_image, il, ie, ll, le,
  72.                  0, 0);
  73.    }  /* ends if 5 */
  74.  
  75.    if(edge_type == 6){
  76.       difference_edge(in_name, out_name, the_image,
  77.                  out_image, il, ie, ll, le,
  78.                  0, 0);
  79.    }  /* ends if 6 */
  80.  
  81.    if(edge_type == 7){
  82.       contrast_edge(in_name, out_name, the_image,
  83.                  out_image, il, ie, ll, le,
  84.                  0, 0);
  85.    }  /* ends if 7 */
  86.  
  87.    if(edge_type == 8){
  88.       gaussian_edge(in_name, out_name, the_image,
  89.                  out_image, il, ie, ll, le,
  90.                  3, 0, 0);
  91.    }  /* ends if 8 */
  92.  
  93.    if(edge_type == 10){
  94.       range(in_name, out_name, the_image,
  95.             out_image, il, ie, ll, le,
  96.             3, 0, 0);
  97.    }  /* ends if 10 */
  98.  
  99.    if(edge_type == 11){
  100.       variance(in_name, out_name, the_image,
  101.                out_image, il, ie, ll, le,
  102.                0, 0);
  103.    }  /* ends if 11 */
  104.  
  105.       /* copy out_image to the_image */
  106.    for(i=0; i<ROWS; i++)
  107.       for(j=0; j<COLS; j++)
  108.          the_image[i][j] = out_image[i][j];
  109.  
  110.       /******************************
  111.       *
  112.       *   Threshold the edge detector
  113.       *   output at a given percent.
  114.       *   This eliminates the weak
  115.       *   edges.
  116.       *
  117.       *******************************/
  118.  
  119.    zero_histogram(histogram);
  120.    calculate_histogram(the_image, histogram);
  121.    find_cutoff_point(histogram, percent, &cutoff);
  122.    threshold_image_array(the_image, out_image,
  123.                          255, cutoff, set_value);
  124.  
  125.    if(erode != 0){
  126.          /* copy out_image to the_image */
  127.       for(i=0; i<ROWS; i++)
  128.          for(j=0; j<COLS; j++)
  129.             the_image[i][j] = out_image[i][j];
  130.       erode_image_array(the_image, out_image,
  131.                         set_value, erode);
  132.    }  /* ends if erode */
  133.  
  134.  
  135.       /*******************************
  136.       *
  137.       *   Set all the edge values to
  138.       *   FORGET_IT so the region
  139.       *   growing will not use those
  140.       *   points.
  141.       *
  142.       *******************************/
  143.  
  144.    for(i=0; i<ROWS; i++)
  145.       for(j=0; j<COLS; j++)
  146.          if(out_image[i][j] == set_value)
  147.             out_image[i][j] = FORGET_IT;
  148.  
  149.    for(i=0; i<ROWS; i++)
  150.       for(j=0; j<COLS; j++)
  151.          the_image[i][j] = out_image[i][j];
  152.  
  153.    pixel_grow(the_image, out_image, diff,
  154.               min_area, max_area);
  155.  
  156.    write_array_into_tiff_image(out_name, out_image,
  157.                                il, ie, ll, le);
  158.  
  159. }  /* ends edge_region */
  160.  
  161.